package com.dhecp.project.api.service.impl;

import com.dhecp.common.config.Global;
import com.dhecp.common.utils.barcode.BarcodeUtil;
import com.dhecp.common.utils.pdf.FreeMarkerPdfUtil;
import com.dhecp.project.api.service.ProductBasisService;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 基础商品库  业务实现层
 * @author DH-Xxj
 * @version 2022年7月13日
 */
@Service
public class ProductBasisServiceImpl implements ProductBasisService {

	@Override
	public String createGoodsPdf(String productCode, String productNameEn, Integer num) {

		productNameEn = productNameEn == null ? "" : productNameEn;

		// 生成条形码图片
		String fileDir = Global.getProfile() + "/barcodeImg";
		File tempDir = new File(fileDir + "/");
		if (!tempDir.exists()) {
			tempDir.mkdirs();
		}

		String fileName = productCode + ".png";
		BufferedImage barCodeImage = BarcodeUtil.createImg(productCode, 500, 200);
		String imgFile = BarcodeUtil.createBarcodeImgFile(barCodeImage, fileDir, fileName);
		System.out.println("imgFile---" + imgFile);

		// 生成PDF
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//这里是给具体的某些字段赋值
		paramMap.put("productCode", productCode);
		paramMap.put("productNameEn", productNameEn);
		paramMap.put("imgFile", "file:" + imgFile);
		paramMap.put("num", num);
		paramMap.put("totalPage", (num + 26) / 27);
		//调用具体的实现方法
		String templatePath = Global.getProfile() + "/pdfmuban/";
		String templateName = "goods.ftl";
		String desDir = Global.getProfile() + "/goodspdf/";
		String tempHtmlName = productCode + ".html";
		String desPdfName = productCode + ".pdf";
		String fontDir = templatePath;
		FreeMarkerPdfUtil.createPdf(templatePath, templateName, desDir, tempHtmlName, desPdfName, fontDir, paramMap);
		return desDir + desPdfName;

	}

}
